//
//  CooperSwift.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/4/27.
//

import Foundation
import SwiftyBeaver  // 日志
import SKPhotoBrowser

//MARK: - log
public let log = SwiftyBeaver.self


public struct CooperSwift {
    
    public static func config() {
        
        // 配置log
        log.addDestination(ConsoleDestination())
        
        // 配置全屏返回
//        SHFullscreenPopGesture.configure()
        
    }

}

// present
public func presentTo(_ vc: UIViewController) {
    UIApplication.currentVC()?.present(vc, animated: true, completion: nil)
}

public func presentTo(_ vcCls: UIViewController.Type) {
    presentTo(vcCls.init())
}

public func presentTo(_ vcName: String) {
    let preStr = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String
    if var _preStr = preStr {
        //如果项目名中有  — 需要做一下 替换 ，否则不需要
        _preStr =  _preStr.replacingOccurrences(of: "-", with: "_")
        guard let vcCls =  NSClassFromString("\(_preStr).\(vcName)") as? UIViewController.Type else { return }
        presentTo(vcCls.init())
    }
}

/// push 到
/// - Parameter vc: target vc
public func pushTo(_ vc: UIViewController) {
    UIApplication.currentVC()?.navigationController?.pushViewController(vc, animated: true)
}

public func pushTo(_ vcCls: UIViewController.Type) {
    pushTo(vcCls.init())
}

public func pushTo(_ vcName: String) {
    let preStr = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String
    if var _preStr = preStr {
        //如果项目名中有  — 需要做一下 替换 ，否则不需要
        _preStr =  _preStr.replacingOccurrences(of: "-", with: "_")
        guard let vcCls =  NSClassFromString("\(_preStr).\(vcName)") as? UIViewController.Type else { return }
        pushTo(vcCls.init())
    }
}

/// pop 返回
public func popTo() {
    UIApplication.currentVC()?.navigationController?.popViewController(animated: true)
}

// MARK: - 显示图片
public func showPhotosBrowser(_ photos: [Any], index currentIdx: Int = 0) {
    let images: [SKPhotoProtocol?] = photos.map {
        var photo: SKPhotoProtocol?
        if let imageStr = $0 as? String {
            if imageStr.count > 4 && imageStr.hasPrefix("http") {
                photo = SKPhoto.photoWithImageURL(imageStr).then { photo in
                   photo.shouldCachePhotoURLImage = false // you can use image cache by true(NSCache)
                }
            }else
            if imageStr.count > 0 {
                photo = SKPhoto.photoWithImage(UIImage(named: imageStr) ?? UIImage.init(color: .lightText, size: .zero)).then { photo in
                   photo.shouldCachePhotoURLImage = false // you can use image cache by true(NSCache)
                }
            }
        }
        if let image = $0 as? UIImage {
            photo = SKPhoto.photoWithImage(image)
        }
        return photo
    }.filter {
        $0 != nil
    }
    if images.count <= 0 {
        return
    }
    let browser = SKPhotoBrowser(photos: images as! [SKPhotoProtocol])
    browser.initializePageIndex(currentIdx)
    UIApplication.currentVC()?.present(browser, animated: true)
}
